package com.ias.redis.config;

import com.ias.redis.support.RedisCacheManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;

@AutoConfiguration
@AutoConfigureAfter(RedisAutoConfiguration.class)
@EnableConfigurationProperties(CacheRedisProperties.class)
@ConditionalOnProperty(prefix = "ias.cache.l1", name = "enabled", havingValue = "true", matchIfMissing = true)
@Slf4j
public class CacheRedisAutoConfiguration {

    @Bean("L1_CacheManager")
    @Primary  // 设置为默认的缓存管理器
    @ConditionalOnMissingBean(name = "L1_CacheManager")
    public RedisCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate,
                                         CacheRedisProperties cacheRedisProperties) {
        log.info("[L1_CacheManager] init as PRIMARY cache manager.");
        return new RedisCacheManager(cacheRedisProperties, redisTemplate);
    }
}